/* EasyWay Game Engine
 * Copyright (C) 2006 Daniele Paggi.
 *  
 * Written by: 2006 Daniele Paggi<dshnt@hotmail.com>
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/**
 * 
 */
package org.easyway.editor.forms.components.extended;

import java.lang.annotation.Annotation;

import org.easyway.annotations.editor.Angle;
import org.easyway.annotations.editor.Height;
import org.easyway.annotations.editor.Info;
import org.easyway.annotations.editor.Resource;
import org.easyway.annotations.editor.Width;
import org.easyway.annotations.editor.X;
import org.easyway.annotations.editor.Y;
import org.easyway.editor.forms.components.BooleanSelector;
import org.easyway.editor.forms.components.Group;
import org.easyway.editor.forms.components.InputText;
import org.easyway.editor.forms.components.interfaces.IValidity;

/**
 * @author Proprietario
 * 
 */
public class GenericAnalyzer {

	boolean getx, gety;

	int idPosition;

	Annotation annotations[][];

	Group group;

	Class types[];

	private boolean getAngle;

	private boolean getWSize, getHSize;

	private int idAngle;

	private int idSize;

	public GenericAnalyzer(Annotation paramsAnn[][], Class params[]) {
		this.group = new Group();
		annotations = paramsAnn;
		types = params;
		GetPointButton.list.clear();
		GetRotationButton.list.clear();
		GetSizeButton.list.clear();
	}

	// ---------------------------
	// ---------------------------

	protected String searchAnnotations(int i) {
		String name = "";
		getx = gety = getAngle = getWSize = getHSize = false;
		for (Annotation a : annotations[i]) {
			if (a instanceof X) {
				System.out.println("getx");
				getx = true;
				idPosition = ((X) a).id();
				if (GetPointButton.findById(idPosition) == null) {
					new GetPointButton(idPosition);
				}
			} else if (a instanceof Y) {
				System.out.println("gety");
				gety = true;
				idPosition = ((Y) a).id();
				if (GetPointButton.findById(idPosition) == null) {
					new GetPointButton(idPosition);
				}
			}
			if (a instanceof Angle) {
				System.out.println("get angle");
				getAngle = true;
				idAngle = ((Angle) a).id();
				if (GetRotationButton.findById(idAngle) == null) {
					new GetRotationButton(idAngle);
				}

			}
			if (a instanceof Width) {
				System.out.println("getWidth");
				getWSize = true;
				idSize = ((Width) a).id();
				if (GetSizeButton.findById(idSize) == null) {
					new GetSizeButton(idSize);
				}
			}
			if (a instanceof Height) {
				System.out.println("getWidth");
				getHSize = true;
				idSize = ((Height) a).id();
				if (GetSizeButton.findById(idSize) == null) {
					new GetSizeButton(idSize);
				}
			}

			if (a instanceof Info) {
				name = ((Info) a).value();
			}
			if (a instanceof Resource) {
				group.add(new LoaderButton(((Resource) a).value(), "" + i));
				return null;
			}
		}
		return name;
	}

	protected void scanParameters() {
		int i = 0;
		for (Class type : types) {
			String name = "";
			name = searchAnnotations(i);
			if (name == null) {
				++i;
				continue;
			}
			if (type.getName().equals("org.easyway.interfaces.base.ITexture")) {
				group.add(new GetTextureButton("texture", "" + i));
			} else if (type.getName().equals("org.easyway.tiles.Tile")) {
				group.add(new GetTileButton("tile", "" + i));
			} else if (type.getName().equals("float")
					|| type.getName().equals("double")) {
				group.add(toAddto(new InputText(name, "" + i, true, true)));
			} else if (type.getName().equals("int")) {
				group.add(toAddto(new InputText(name, "" + i, true, false)));
			} else if (type.getName().equals("java.lang.String")) {
				group.add(toAddto(new InputText(name, "" + i, false)));
			} else if (type.getName().equals("boolean")) {
				new BooleanSelector(name, "" + i);
			} else {
				System.out
					.println("Unknowed Parameter Type: " + type.getName());
			}
			++i;
		}
	}

	protected InputText toAddto(InputText it) {
		if (getx) {
			GetPointButton.findById(idPosition).inputx = it;
		}
		if (gety) {
			GetPointButton.findById(idPosition).inputy = it;
		}
		if (getAngle) {
			GetRotationButton.findById(idAngle).inputRot = it;
		}
		if (getWSize) {
			GetSizeButton.findById(idSize).inputWidth = it;
		}
		if (getHSize) {
			GetSizeButton.findById(idSize).inputHeight = it;
		}
		return it;
	}

	// ---------------------------
	// ---------------------------

	protected Object[] parseInputText(Object parameters[], int index,
			InputText it) {
		if (it.isNumber == false) {
			parameters[index] = it.getValue();
		} else if (it.isFloat == false) {
			parameters[index] = new Integer(it.getInt());
		} else
			parameters[index] = new Float(it.getFloat());

		return parameters;
	}

	public Object[] getFinalParameters() {
		Object parameters[] = new Object[types.length];
		for (int i = 0; i < parameters.length; ++i) {
			for (IValidity it : Group.AllObjects) {
				if (Integer.parseInt(it.getName()) == i) {
					if (it instanceof InputText) {
						parseInputText(parameters, i, (InputText) it);
					}
					if (it instanceof GetTextureButton) {
						parameters[i] = ((GetTextureButton) it).texture;
					}
					if (it instanceof GetTileButton) {
						parameters[i] = ((GetTileButton)it).tile;
					}
					if (it instanceof BooleanSelector) {
						parameters[i] = ((BooleanSelector) it).getValue();
					}
					if (it instanceof LoaderButton) {
						parameters[i] = ((LoaderButton) it).getValue();
					}
				}
			}
		}
		return parameters;
	}
}
